package com.ruoyi.lwpq.service.impl;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.lwpq.domain.LwpqGzjl;
import com.ruoyi.lwpq.domain.LwpqJl;
import com.ruoyi.lwpq.domain.LwpqJyjl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.lwpq.mapper.LwpqYgglMapper;
import com.ruoyi.lwpq.domain.LwpqYggl;
import com.ruoyi.lwpq.service.ILwpqYgglService;
import com.ruoyi.common.core.text.Convert;

import javax.validation.Validator;

/**
 * 单位用工管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-03-03
 */
@Service
public class LwpqYgglServiceImpl implements ILwpqYgglService 
{
    @Autowired
    private LwpqYgglMapper lwpqYgglMapper;
    @Autowired
    protected Validator validator;

    /**
     * 查询单位用工管理
     * 
     * @param id 单位用工管理主键
     * @return 单位用工管理
     */
    @Override
    public LwpqYggl selectLwpqYgglById(Long id)
    {
        return lwpqYgglMapper.selectLwpqYgglById(id);
    }

    /**
     * 查询单位用工管理列表
     * 
     * @param lwpqYggl 单位用工管理
     * @return 单位用工管理
     */
    @Override
    public List<LwpqYggl> selectLwpqYgglList(LwpqYggl lwpqYggl)
    {
        return lwpqYgglMapper.selectLwpqYgglList(lwpqYggl);
    }

    /**
     * 新增单位用工管理
     * 
     * @param lwpqYggl 单位用工管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertLwpqYggl(LwpqYggl lwpqYggl)
    {
        String birthday=lwpqYggl.getSfzh().substring(6, 14);
        boolean b = Integer.parseInt(lwpqYggl.getSfzh().substring(16, 17)) % 2 == 0;
        if (b) {lwpqYggl.setSex("1");}else {lwpqYggl.setSex("0");}

        lwpqYggl.setCreateTime(DateUtils.getNowDate());
        int rows = lwpqYgglMapper.insertLwpqYggl(lwpqYggl);
        insertLwpqGzjl(lwpqYggl);
        insertLwpqJyjl(lwpqYggl);
        return rows;
    }

    /**
     * 修改单位用工管理
     * 
     * @param lwpqYggl 单位用工管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateLwpqYggl(LwpqYggl lwpqYggl)
    {
        lwpqYggl.setUpdateTime(DateUtils.getNowDate());
        lwpqYgglMapper.deleteLwpqGzjlByYgglId(lwpqYggl.getId());
        lwpqYgglMapper.deleteLwpqJyjlByYgglId(lwpqYggl.getId());
        insertLwpqGzjl(lwpqYggl);
        insertLwpqJyjl(lwpqYggl);

        return lwpqYgglMapper.updateLwpqYggl(lwpqYggl);
    }

    /**
     * 批量删除单位用工管理
     * 
     * @param ids 需要删除的单位用工管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteLwpqYgglByIds(String ids)
    {
        lwpqYgglMapper.deleteLwpqGzjlByYgglIds(Convert.toStrArray(ids));
        lwpqYgglMapper.deleteLwpqJyjlByYgglIds(Convert.toStrArray(ids));
        return lwpqYgglMapper.deleteLwpqYgglByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除单位用工管理信息
     * 
     * @param id 单位用工管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteLwpqYgglById(Long id)
    {
        lwpqYgglMapper.deleteLwpqGzjlByYgglId(id);
        lwpqYgglMapper.deleteLwpqJyjlByYgglId(id);
        return lwpqYgglMapper.deleteLwpqYgglById(id);
    }

    @Override
    public String importYggl(List<LwpqYggl> lwpqyggl, boolean updateSupport, String loginName) {
        if (StringUtils.isNull(lwpqyggl) || lwpqyggl.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (LwpqYggl lw : lwpqyggl)
        {
            try
            {
                // 验证是否存在这个用户

                BeanValidators.validateWithException(validator, lw);
                lw.setCreateBy(loginName);
                this.insertLwpqYggl(lw);
                successNum++;
                successMsg.append("<br/>" + successNum + "、账号 " + lw.getName() + " 导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、账号 " + lw.getName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    /**
     * 新增工作经历信息
     * 
     * @param lwpqYggl 单位用工管理对象
     */
    public void insertLwpqGzjl(LwpqYggl lwpqYggl)
    {
        List<LwpqGzjl> lwpqGzjlList = lwpqYggl.getLwpqGzjlList();
        Long id = lwpqYggl.getId();
        if (StringUtils.isNotNull(lwpqGzjlList))
        {
            List<LwpqGzjl> list = new ArrayList<LwpqGzjl>();
            for (LwpqGzjl lwpqGzjl : lwpqGzjlList)
            {
                lwpqGzjl.setYgglId(id);
                list.add(lwpqGzjl);
            }
            if (list.size() > 0)
            {
                lwpqYgglMapper.batchLwpqGzjl(list);
            }
        }
    }

    /**
     * 新增教育经历信息
     *
     * @param lwpqYggl 单位用工管理对象
     */
    public void insertLwpqJyjl(LwpqYggl lwpqYggl)
    {
        List<LwpqJyjl> lwpqJyjlList = lwpqYggl.getLwpqJyjlList();
        Long id = lwpqYggl.getId();
        if (StringUtils.isNotNull(lwpqJyjlList))
        {
            List<LwpqJyjl> list = new ArrayList<LwpqJyjl>();
            for (LwpqJyjl lwpqJyjl : lwpqJyjlList)
            {
                lwpqJyjl.setYgglId(id);
                list.add(lwpqJyjl);
            }
            if (list.size() > 0)
            {
                lwpqYgglMapper.batchLwpqJyjl(list);
            }
        }
    }
}
